Ext.ns('vali.validator');

vali.validator = function() {
	var errors = new Array();
	
	this.addErrors = function(ctrlId, msg) {
		var error = new Object();
		
		error.key = ctrlId;
		error.value = msg;
		
		errors.push(error);
	}
	
	this.clear = function(errorsId) {
		$("#" + errorsId).empty();
		$(".input-error").removeClass("input-error");
		$("#" + errorsId).hide();
		
		errors = new Array();
	}
	
	this.getErrors = function() {
		return errors;
	}
	
	this.getErrorsCount = function() {
		return errors.length;
	}
	
	this.writeErrors = function(errorsId) {
		var html = "";
		
		$(".input-error").removeClass("input-error");
			
		for(var i = 0; i < errors.length; i++) {
			if(errors[i].key.trim() != "" )
				$("#" + errors[i].key).addClass("input-error");
			
			if(errors[i].value.trim() != "") {
				html += "<li>" + errors[i].value + "</li>";
			}
		}
		
		$("#" + errorsId).html(html);
		$("#" + errorsId).show();
	}
	
	this.isEmpty = function(text) {
		
		return text == null || text == '';
	}
	
	this.isEmptyField = function(id) {
		var text = $("#" + id).val();
		
		return this.isEmpty(text);
	}

	this.isNumber = function(text) {
		var pattern = /^\d+$/;
		return pattern.test(text);
	}
	
	this.isNumberField = function(id) {	
		var text = $("#" + id).val();
		
		return this.isNumber(text);
	}

	
	this.isEmail = function(text) {
		var pattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/;
		return pattern.test(text);
	}
	
	this.isEmailField = function(id) {
		var text = $("#" + id).val();
		
		return this.isEmail(text);
	}
	
	this.isAlphabet = function(text) {
		var pattern = /^[a-zA-Z0-9]+$/;
		return pattern.test(text);
	}
	
	this.isAlphabetField = function(id) {
		var text = $("#" + id).val();
		
		return this.isAlphabet(text);
	}
	
	this.isValidChar = function(text, minChar, maxChar) {
		return (text.length >= minChar && text.length <= maxChar);
	}
	
	this.isValidCharField = function(id, minChar, maxChar) {
		var text = $("#" + id).val();
		
		return this.isValidChar(text, minChar, maxChar);
	}
	
	this.isValidByte = function(text, minByte, maxByte) {
		var len = 0;
	    
	    for (var i = 0; i < text.length; ++i) {
	      var c = text.charCodeAt(i);
	      
	      if (c < 256 || (c >= 0xff61 && c <= 0xff9f)) {
	        len = len + 1;
	      }
	      else {
	    	  len = len + 2;
	      }
	    }
	    return (len >= minByte && len <= maxByte);
	}
	
	this.isValidByteField = function(id, minByte, maxByte) {
		var text = $("#" + id).val();
		
		return this.isValidByte(text, minByte, maxByte);
	}
	
	this.compare = function(v1, v2) {
		return v1 == v2;
	}
	
	this.compareField = function(f1, f2) {
		return this.compare($("#" + f1).val(), $("#" + f2).val());
	}
	
	this.isValidUsername = function(text) {
		var pattern = /^[a-zA-Z0-9._-]+$/;
		return pattern.test(text);
	}
	
	this.isValidUsernameField = function(id) {
		var text = $("#" + id).val();
		
		return this.isValidUsername(text);
	}
	
	this.isValidPassword = function(text) {
		var pattern = /^[A-Za-z0-9\!\#\$\%\&]+$/;
		return pattern.test(text);
	}
	
	this.isValidPasswordField = function(id) {
		var text = $("#" + id).val();
		
		return this.isValidPassword(text);
	}
}